# 定义颜色
GREEN  := \033[0;32m
YELLOW := \033[1;33m
RED    := \033[0;31m
BLUE   := \033[0;34m
NC     := \033[0m

# 设置静态编译环境变量
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64

# 编译优化选项
GO_BUILD_FLAGS := -trimpath
GO_LD_FLAGS := -s -w

# 声明所有伪目标
.PHONY: help all clean build run test deps version

# 默认目标
help:
	@echo "$(YELLOW)可用命令:$(NC)"
	@echo "  $(GREEN)all$(NC)      - 清理并构建应用"
	@echo "  $(GREEN)clean$(NC)    - 清理构建缓存"
	@echo "  $(GREEN)build$(NC)    - 构建应用"
	@echo "  $(GREEN)run$(NC)      - 运行应用"
	@echo "  $(GREEN)test$(NC)     - 运行测试"
	@echo "  $(GREEN)deps$(NC)     - 检查依赖"
	@echo "  $(GREEN)version$(NC)  - 显示版本信息"
	@echo "  $(GREEN)help$(NC)     - 显示帮助信息"

# 检测操作系统
ifeq ($(OS),Windows_NT)
    SHELL := cmd.exe
    BINARY_SUFFIX := .exe
    RM_F := del /f /q
    RM_RF := rmdir /s /q
    MKDIR := mkdir
    BUILD_TIME := $(shell powershell Get-Date -Format "yyyy-MM-dd_HH:mm:ss")
else
    BINARY_SUFFIX :=
    RM_F := rm -f
    RM_RF := rm -rf
    MKDIR := mkdir -p
    BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
endif

# 构建参数
MAIN_PATH := .
BINARY_PATH := bin
VERSION := $(shell git describe --tags --always)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GO_VERSION := $(shell go version)
BUILDER := $(shell whoami)@$(shell hostname)

# 编译参数
LDFLAGS := -ldflags "\
	${GO_LD_FLAGS} \
	-X 'gitee.com/rw555/fdsmastergo/pkg/version.version=${VERSION}' \
	-X 'gitee.com/rw555/fdsmastergo/pkg/version.buildTime=${BUILD_TIME}' \
	-X 'gitee.com/rw555/fdsmastergo/pkg/version.gitCommit=${GIT_COMMIT}' \
	-X 'gitee.com/rw555/fdsmastergo/pkg/version.goVersion=${GO_VERSION}' \
	-X 'gitee.com/rw555/fdsmastergo/pkg/version.builder=${BUILDER}'"

# 在文件开头添加版本信息显示函数
define show_version
	@echo "$(YELLOW)版本信息:$(NC)"
	@echo "  $(GREEN)版本号:$(NC) ${VERSION}"
	@echo "  $(GREEN)Git提交:$(NC) ${GIT_COMMIT}"
	@echo "  $(GREEN)构建时间:$(NC) ${BUILD_TIME}"
	@echo "  $(GREEN)Go版本:$(NC) ${GO_VERSION}"
	@echo "  $(GREEN)构建者:$(NC) ${BUILDER}"
endef

all: clean build

clean:
ifeq ($(OS),Windows_NT)
	@if exist "$(BINARY_PATH)" $(RM_RF) "$(BINARY_PATH)"
else
	@$(RM_RF) $(BINARY_PATH)
endif
	@echo "$(YELLOW)清理构建缓存...$(NC)"

build:
	@echo "$(YELLOW)构建 $(GREEN)$(APP_NAME)$(NC)..."
ifeq ($(OS),Windows_NT)
	@if not exist "$(BINARY_PATH)" $(MKDIR) "$(BINARY_PATH)"
	@set CGO_ENABLED=0 && go build $(GO_BUILD_FLAGS) $(LDFLAGS) -o "$(BINARY_PATH)/$(APP_NAME)$(BINARY_SUFFIX)" "./$(MAIN_PATH)"
else
	@$(MKDIR) $(BINARY_PATH)
	go build $(GO_BUILD_FLAGS) $(LDFLAGS) -o "$(BINARY_PATH)/$(APP_NAME)$(BINARY_SUFFIX)" "./$(MAIN_PATH)"
endif
	@echo "$(GREEN)构建完成: $(BINARY_PATH)/$(APP_NAME)$(BINARY_SUFFIX)$(NC)"

run: build
	@echo "$(YELLOW)运行 $(GREEN)$(APP_NAME)$(NC)..."
ifeq ($(OS),Windows_NT)
	@cd $(BINARY_PATH) && $(APP_NAME)$(BINARY_SUFFIX)
else
	@cd $(BINARY_PATH) && ./$(APP_NAME)$(BINARY_SUFFIX)
endif

test:
	@echo "$(YELLOW)运行测试...$(NC)"
	@cd ../../ && go test -v ./internal/... ./pkg/...
	@echo "$(GREEN)测试完成$(NC)"

deps:
	@echo "$(YELLOW)检查依赖...$(NC)"
	@go mod tidy
	@go mod verify
	@echo "$(GREEN)依赖检查完成$(NC)"

version:
	$(call show_version) 